package com.hd.trans.db.bean

import com.hd.trans.db.DataBaseMgr
import com.hd.trans.db.greenDao.AiChatRecordDao
import com.hd.trans.network.bean.aiChat.AIAnswer
import com.hd.trans.network.bean.aiChat.AIDataType
import com.hd.trans.network.bean.aiChat.TaskStatus

object AiChatRecordDataMgr {
    fun saveAIAnswer(aiAnswer: AIAnswer) {
        val aiChatRecord = makeupAiChatRecord(aiAnswer)
        saveAiChatRecord(aiChatRecord)
    }

    fun saveAiChatRecord(aiChatRecord: AiChatRecord) {
        DataBaseMgr.getInstance().daoSession.aiChatRecordDao.insertOrReplace(aiChatRecord)
    }

    fun deleteAiChatRecord(aiChatRecord: AiChatRecord) {
        DataBaseMgr.getInstance().daoSession.aiChatRecordDao.delete(aiChatRecord)
    }

    fun getAllAiChatRecord(): List<AiChatRecord> {
        return DataBaseMgr.getInstance().daoSession.aiChatRecordDao.loadAll()
    }

    fun getAllAIAnswer(): List<AIAnswer> {
        val aiChatRecords = getAllAiChatRecord()
        val aiAnswers = mutableListOf<AIAnswer>()
        aiChatRecords.forEach {
            aiAnswers.add(makeupAIAnswer(it))
        }
        return aiAnswers
    }

    fun getAllAiChatRecordByScene(scene: String): List<AiChatRecord> {
        return DataBaseMgr.getInstance().daoSession.aiChatRecordDao.loadAll().filter {
            it.obligate == scene
        }
    }

    fun getAllAIAnswerByScene(scene: String): List<AIAnswer> {
        val aiChatRecords = getAllAiChatRecordByScene(scene)
        val aiAnswers = mutableListOf<AIAnswer>()
        aiChatRecords.forEach {
            aiAnswers.add(makeupAIAnswer(it))
        }
        return aiAnswers
    }

    fun getAiChatRecordByCreateTime(createTime: Long): AiChatRecord? {
        return DataBaseMgr.getInstance().daoSession.aiChatRecordDao.queryBuilder()
            .where(AiChatRecordDao.Properties.CreateTime.eq(createTime)).unique()
    }

    fun deleteAllAiChatRecord() {
        DataBaseMgr.getInstance().daoSession.aiChatRecordDao.deleteAll()
    }

    fun deleteAiChatRecordByCreateTime(createTime: Long) {
        val aiChatRecord = getAiChatRecordByCreateTime(createTime)
        if (aiChatRecord != null) {
            deleteAiChatRecord(aiChatRecord)
        }
    }

    fun makeupAiChatRecord(aiAnswer: AIAnswer): AiChatRecord {
        val aiChatRecord = AiChatRecord()
        aiChatRecord.id = aiAnswer.createTime
        aiChatRecord.createTime = aiAnswer.createTime
        aiChatRecord.dataType = aiAnswer.dataType.dataType
        aiChatRecord.content = aiAnswer.output
        aiChatRecord.status = aiAnswer.status.value
        aiChatRecord.obligate = aiAnswer.scene.scene
        aiChatRecord.headUrl = aiAnswer.headUrl
        return aiChatRecord
    }

    fun makeupAIAnswer(aiChatRecord: AiChatRecord): AIAnswer {
        val createTime = aiChatRecord.createTime
        val dataType =
            if (aiChatRecord.dataType == AIDataType.ANSWER.dataType) AIDataType.ANSWER else AIDataType.QUESTION
        val output = aiChatRecord.content
        val headUrl = aiChatRecord.headUrl
        return AIAnswer(output,
            dataType,
            status = TaskStatus.DONE,
            headUrl = headUrl,
            createTime = createTime)
    }
}